library String

    struct String
        string m_value
        integer m_length
        integer m_arrLength
        //单个数组元素含有数字的个数
        integer m_arrElementSize
        private string array m_strs[16]

        public method operator [] takes integer n returns string
            return this.m_strs[n]
        endmethod

        public method operator []= takes integer n, string str returns nothing
            set this.m_strs[n] = str
            set this.m_arrLength = this.m_arrLength + 1
        endmethod

        static method create takes string value returns String
            local String this = String.allocate()
            set this.m_value = value
            set this.m_length = StringLength(value)
            return this
        endmethod

        method destroy takes nothing returns nothing
            local integer i
            set i = 0
            loop
                exitwhen i >= this.m_length
                set this[i] = null
                set i = i + 1
            endloop
            set this.m_value = null
            set this.m_arrLength = 0
            set this.m_arrElementSize = 0
            call this.deallocate()
        endmethod

        method toArray takes integer size returns nothing
            local integer i
            local integer index = 0
            if size <= 0 then
                return
            endif
            if size > this.m_length then
                set this[index] = this.m_value
                return
            endif
            set this.m_arrElementSize = size
            set i = 0
            loop
                exitwhen i >= this.m_length
                if (i + size) <= this.m_length then
                    set this[index] = SubString(this.m_value, i, i + size)
                else
                    set this[index] = SubString(this.m_value, i, this.m_length)
                endif
                set index = index + 1
                set i = i + size
            endloop
        endmethod

        method toArrayString takes integer size returns string
            local string str = ""
            local integer i
            call this.toArray(size)
            set i = 0
            loop
                exitwhen i >= this.m_arrLength
                if i == this.m_arrLength - 1 then
                    set str = str + (this[i])
                else
                    set str = str + (this[i] + ",")
                endif
                set i = i + 1
            endloop
            return str
        endmethod

        //adds a string to another string in spaced intervals
        //StringJoin("1234567","-",3,0) -> "123-456-7"
        static method join takes string s, string str, integer spacing, integer start returns string
            local integer i = StringLength(s)
            local integer p
            set p = 1
            loop
                exitwhen p * spacing + start >= i
                set s = SubString(s, 0, p * spacing + p + start - 1) + str + SubString(s, p * spacing + p + start - 1, StringLength(s))
                set p = p + 1
            endloop
            return s
        endmethod
    endstruct

endlibrary